<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        margin: 0;
        height: 100vh;
      }
      div {
        margin: 0;
        padding: 0;
        height: 100%;
        overflow-y: scroll; /* 确保 div 可以滚动 */
      }
      p{
        height: 300px;
        line-height: 300px;
      }
    </style>
  </head>
  <body>
    <!-- 节流测试 -->
    <div id="divId">
      <!-- 添加一些内容来触发滚动 -->
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
      <p>滚动测试内容</p>
    </div>
  </body>
</html>
<script>
  // 节流
  function throttle(fn, delay) {
    let lastTime = 0;
    return (...args) => {
      const now = Date.now();
      if (now - lastTime > delay) {
        fn.apply(this, args);
        lastTime = now;
      }
    };
  }
  let divId = document.getElementById("divId");

//   const fn0 = () => {
//     console.log("非节流");
//   };

//   divId.addEventListener(
//     "scroll",
//     fn0
//   );

  const fn = () => {
    console.log("节流");
  };
  divId.addEventListener(
    "scroll",
    throttle(fn, 1000)
  );
</script>